package syt.control;



import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import algorithm.structure.tree.b.BTreeSYT;
import algorithm.structure.tree.br.BRTreeSyt;
import algorithm.structure.tree.util.TreeTool;

/**
 * Servlet implementation class SYTservlet
 */
public class SYTservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
     
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SYTservlet() {
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		String xml = "<?xml version='1.0' encoding='iso-8859-1'?>" + 
					 "<tree id=\"0\">" + 
					 	"<item text=\"Books\" id=\"books\" >" + 
					 		"<item text=\"Mystery &amp; Thrillers\" id=\"mystery\" >" + 
					 			"<item text=\"Lawrence Block\" id=\"lb\" >" + 
					 				"<item text=\"All the Flowers Are Dying\" id=\"lb_1\" />" + 
					 				"<item text=\"The Burglar on the Prowl\" id=\"lb_2\" />" + 
					 				"<item text=\"The Plot Thickens\" id=\"lb_3\" />" + 
					 				"<item text=\"Grifter's Game\" id=\"lb_4\" />" + 
					 				"<item text=\"The Burglar Who Thought He Was Bogart\" id=\"lb_5\" />" + 
					 			"</item>" + 
					 		"</item>" + 
					 	"</item>" + 
					 "</tree>";
	
		
		int[] a = {1,2,3,4,5,6,7,8,9,10,11,12,13,14}; 
		BRTreeSyt tree = new BRTreeSyt();
		for(int i = 0; i < a.length; i++){
			tree.addNode(a[i]);
		}
		tree.deleteNode(2);
		//System.out.println(TreeTool.getInstanse().OSSelect(tree.getRoot(), 5).getNodeKey().getValue());
		System.out.println(TreeTool.getInstanse().OsRank(tree,5));
			*/
		BTreeSYT tree = new BTreeSYT();
		tree.insert(1);
		tree.insert(2);
		tree.insert(3);
		tree.insert(4);
		tree.insert(5);
		tree.insert(6);
		tree.insert(7);
		tree.insert(8);
		tree.insert(9);
		tree.insert(10);
		tree.insert(11);
		tree.insert(12);
		tree.insert(13);
		tree.insert(14);
		tree.insert(15);
		tree.insert(16);
		tree.insert(17);
//		tree.insert(18);
//		tree.insert(19);
//		tree.insert(20);
//		tree.insert(21);
//		tree.insert(22);
		tree.deleteNode(8);
		tree.insert(8);
		tree.deleteNode(8);
		/*
		tree.deleteNode(1);
		tree.deleteNode(2);
		tree.deleteNode(3);
		tree.deleteNode(4);
		tree.deleteNode(5);
		tree.deleteNode(6);
		tree.deleteNode(7);
		tree.deleteNode(8);
		tree.deleteNode(9);
		tree.deleteNode(10);
		tree.deleteNode(11);
		tree.deleteNode(12);
		tree.deleteNode(13);
		tree.deleteNode(14);
		tree.deleteNode(15);
		tree.deleteNode(16);
		*/
		tree.deleteNode(17);
		tree.deleteNode(16);
		tree.deleteNode(15);
		tree.deleteNode(13);
		tree.deleteNode(12);
		tree.deleteNode(11);
		tree.deleteNode(10);
		tree.deleteNode(9);
		tree.deleteNode(8);
		tree.deleteNode(7);
		tree.deleteNode(6);
		tree.deleteNode(5);
		tree.deleteNode(4);
		tree.deleteNode(3);
		tree.deleteNode(2);
//		tree.deleteNode(1);
		
		
		
		
		
		String xml = TreeTool.getInstanse().getBTreeXml(tree);
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		out.print(xml);
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
